package com.icss.mqsp.util;

import com.icss.j2ee.util.Md5;
import com.icss.mqsp.db.DBManager;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DataUtil
{
  public static String listToJson1(List<Map<String, String>> dataList)
  {
    String result = "";
    JSONArray json = new JSONArray();
    json.addAll(dataList);
    if (json.isEmpty()) {
      System.out.println("no data!!!");
    }
    ListIterator li = json.listIterator();
    while (li.hasNext()) {
      result = result + li.next() + ",";
    }
    result = result.substring(0, result.length() - 1);
    return result;
  }
  
  public static String listToJson(List<Map<String, String>> dataList)
  {
    JSONArray json = new JSONArray();
    for (int i = 0; i < dataList.size(); i++) {
      json.add(i, dataList.get(i));
    }
    return json.toString();
  }
  
  public static JSONArray listToJsonObj(List<Map<String, String>> dataList)
  {
    JSONArray json = new JSONArray();
    for (int i = 0; i < dataList.size(); i++) {
      json.add(i, dataList.get(i));
    }
    return json;
  }
  
  public static Map<String, String> jsonToMap(String s)
  {
    Map<String, String> map = new HashMap();
    JSONObject json = JSONObject.fromObject(s);
    Iterator keys = json.keys();
    while (keys.hasNext())
    {
      String key = (String)keys.next();
      String value = json.get(key).toString();
      value = getStaticParams(key, value);
      map.put(key, value);
    }
    return map;
  }
  
  public static String getStaticParams(String key, String value)
  {
    if (key.equals("D_MONTH"))
    {
      Map<String, String> conditionMap = new HashMap();
      conditionMap.put("time", getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      List rsList = DBManager.queryForList("getMaxDay", conditionMap);
      if (rsList.size() > 0)
      {
        Map map = (Map)rsList.get(0);
        String dvalue = (String)map.get("DDAY");
        


        value = dvalue.substring(0, 6);
      }
    }
    return value;
  }
  
  public static String getCurrentDate(String format)
  {
    String defaultValue = null;
    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    defaultValue = sdf.format(dt);
    return defaultValue;
  }
  
  public static String setPwdJM(String oldPwd)
  {
    String newPwd = null;
    Md5 m = new Md5(oldPwd);
    m.processString();
    newPwd = "MD" + m.getStringDigest();
    return newPwd;
  }
}
